package com.js.nickcar.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.design.widget.TextInputLayout;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.js.nickcar.R;
import com.zyzd.commontools.screen.TDevice;

/**
 * Created by LZY on 2017/6/20.
 */

public class EditDialog extends Dialog implements View.OnClickListener {

    private TextView mTvTitle;
    private EditText mEtContent;
    private TextView mTvConfirm;
    private TextView mTvCancle;

    public EditDialog(Context context) {
        this(context, R.style.dialog_wheel);

    }

    public EditDialog(Context context, int themeResId) {
        super(context, themeResId);
        // 去掉标题，必须在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = getLayoutInflater().inflate(getLayoutId(), null);
        initView(contentView);
        setContentView(contentView);
        // 设置Dialog的宽度等于屏幕的宽度，必须在setContentView后调用
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = TDevice.getWidth() * 3 / 4;
        p.gravity = Gravity.CENTER_VERTICAL;

        getWindow().setAttributes(p);
        getWindow().setWindowAnimations(R.style.AnimBottom);
//        getWindow().getDecorView().setSystemUiVisibility(
//                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//        );

        // 让Dialog显示在底部
        getWindow().setGravity(Gravity.CENTER);
    }

    private int getLayoutId() {
        return R.layout.dialog_edit;
    }

    private void initView(View contentView) {
        mTvTitle = (TextView) contentView.findViewById(R.id.tv_title);
        mEtContent = (EditText) contentView.findViewById(R.id.tv_content);
        mTvConfirm = (TextView) contentView.findViewById(R.id.tv_confirm);
        mTvCancle = (TextView) contentView.findViewById(R.id.tv_cancel);
        mTvConfirm.setOnClickListener(this);
        mTvCancle.setOnClickListener(this);
        mEtContent.setFocusable(true);
        mEtContent.setFocusableInTouchMode(true);
        mEtContent.setImeOptions(EditorInfo.IME_ACTION_GO);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                onConfirm();
                break;
            case R.id.tv_cancel:
                onCancle();
                break;
        }
    }

    /**
     * 取消
     */
    private void onCancle() {
        cancel();
    }

    private void onConfirm() {

        if (onConfirmListener != null) {
            onConfirmListener.onConfirm(mEtContent.getText().toString());
        }
        cancel();
    }

    private OnConfirmListener onConfirmListener;

    public EditDialog setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
        return this;
    }

    public EditDialog setTitle(String title) {
        mTvTitle.setText(title);
        return this;
    }

    public EditDialog setInputType(int type){
        mEtContent.setInputType(type);
        return this;
    }

    public EditDialog setContent(String content) {
        mEtContent.setText(content);
        return this;
    }

    public EditDialog setHint(String contentHit) {
        mEtContent.setHint(contentHit);
        return this;
    }

    @Override
    public void show() {
        super.show();
        mEtContent.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mEtContent != null) {
                    mEtContent.selectAll();
                    TDevice.showSoftKeyboard(mEtContent);
                }
            }
        }, 300);
    }

    public interface OnConfirmListener {
        void onConfirm(String content);
    }


}
